package arina.sh.ethernetcontrol;

import java.io.IOException;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

public class tang_tret_activity extends Activity {
	

	Relay relay250;
	private Handler handlerResponse250 ;
	System_Device system;
	NETWORK_STATUS mNETWORK_STATUS;
	Boolean loopHandle=true;
	int timepost;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.tang_tret);
		
		//PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,, PendingIntent.FLAG_UPDATE_CURRENT);
		Bundle bSystem=getIntent().getExtras();
		if(bSystem!=null){
			system=bSystem.getParcelable("system");
			timepost=bSystem.getInt("timepost");
			
			mNETWORK_STATUS= NETWORK_STATUS.values()[bSystem.getInt("network")];
		}
		else
			return;
		
		
		ImageView bg=(ImageView)findViewById(R.id.bg_image_tang_tret);
		bg.setOnTouchListener(new OnSwipeTouchListener() {
		    @Override
			public void onSwipeTop() {
		        Toast.makeText(getApplicationContext(), "top", Toast.LENGTH_SHORT).show();
		    }
		    @Override
			public void onSwipeRight() {
		    	Intent i = new Intent(tang_tret_activity.this,EthernetActivity.class);
//		    	i.putExtra("system", system);
//		    	i.putExtra("network", mNETWORK_STATUS.ordinal());
//		    	i.putExtra("timepost", timepost);
				startActivity(i);
		    }
		    @Override
			public void onSwipeLeft() {
		    	
		    }
		    @Override
			public void onSwipeBottom() {
		        Toast.makeText(getApplicationContext(), "bottom", Toast.LENGTH_SHORT).show();
		    }
		});
		
		relay250=new Relay(system, 250,6);
		
		//relay250 250
		relay250.ArrButton[0]=(myToggleButton)findViewById(R.id.toggle_dentruoc);
		
		relay250.ArrButton[1]=(myToggleButton)findViewById(R.id.toggle_beptu);
		
		relay250.ArrButton[2]=(myToggleButton)findViewById(R.id.toggle_dennhaxe);
		
		relay250.ArrButton[3]=(myToggleButton)findViewById(R.id.toggle_ocam);
		
		for(int i=0;i<relay250.ArrButton.length-1;i++)
		{
			if(relay250.ArrButton[i]!=null)
			{
				final myToggleButton currImageButton= relay250.ArrButton[i];
				final int index=i;
				currImageButton.setOnClickListener(new View.OnClickListener() {
					
					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						sendUDPinBackground sendUDP=new	sendUDPinBackground();
						if(currImageButton.isChecked()==false)
						{
							sendUDP.execute(relay250,index+1,true);
						}
						else
						{
							sendUDP.execute(relay250,index+1,false);
						}
					}
				});	

			}
		}
		Runnable runable250=new Runnable() {
			
			@Override
			public void run() {
				// TODO Auto-generated method
				if(mNETWORK_STATUS!=NETWORK_STATUS.NOT_CONNECTED)
				{
					try {
						relay250.readStatus(mNETWORK_STATUS);
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					
					handleUIwithResponse handleUI250=new handleUIwithResponse();
					handleUI250.execute(new Relay[]{relay250});
	
					if(loopHandle)
					{
						handlerResponse250.postDelayed(this, timepost);
						
					}
				}
				else
					Toast.makeText(getApplicationContext(), mNETWORK_STATUS.toString(), Toast.LENGTH_SHORT).show();
			}
		};	
		handlerResponse250=new Handler();
		handlerResponse250.postDelayed(runable250, 200);
	}
	private class sendUDPinBackground extends AsyncTask<Object, Void, Void>
	{

		@Override
		protected Void doInBackground(Object... params) {
			// TODO Auto-generated method stub
			Relay relay=(Relay)params[0];
			int channel=(Integer)params[1];
			Boolean isON=(Boolean)params[2];
			if(isON)
			{
				relay.On(channel, mNETWORK_STATUS);
				return null;
			}
			relay.Off(channel, mNETWORK_STATUS);
			return null;
			
		}
		
	}
	private class handleUIwithResponse extends AsyncTask<Relay, Void, String>{
		
		@Override
		protected String doInBackground(Relay...relay){
			if(relay[0].Response!=null)
			{
				String strResponse=relay[0].getID()+relay[0].Response;
				
				return strResponse;

			}
			return null;
			}
		@Override
	    protected void onPostExecute(String result) {
			if(result!=null)
			{
				
				String strID=result.substring(0, 3);
				result=result.substring(3);
				int iID= Integer.parseInt(strID);
				switch (iID) {
				case 250:
					helperClass.updateUI(result.toCharArray(), relay250.ArrButton);
					break;
			
				default:
					break;
				}
				
			}
	    }	
			
	}
	
	@Override
	protected void onResume(){
		super.onResume();
		
		loopHandle=true;

	}
	

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		
		loopHandle=false;
	}
	

}
